package com.wyzz.hr.action;

import java.util.Iterator;
import java.util.List;
import org.apache.axis.utils.StringUtils;
import org.hibernate.criterion.DetachedCriteria;
import org.hibernate.criterion.Restrictions;

import com.wyzz.hr.base.BaseDownloadAction;
import com.wyzz.hr.bo.IAttdSyncRecordBO;
import com.wyzz.hr.pojo.AttdMachine;
import com.wyzz.hr.pojo.AttdSyncRecord;
import com.wyzz.hr.pojo.Employee;
import com.wyzz.hr.spring.SpringBeanFactory;
import com.wyzz.hr.util.BaseCrit;
import com.wyzz.hr.util.Pager;

public class AttdSyncRecordShowAction extends BaseDownloadAction {

	public AttdSyncRecordShowAction() {
		departmentList = null;
		machineList = null;
		page = new Pager();
		searchOrExport = null;
	}

	public String execute() throws Exception {
		getDrillDownList();
		attdSyncRecordList = searchattdSyncRecord();
		if ("export".equals(searchOrExport)) {
			if (attdSyncRecordList == null || attdSyncRecordList.size() == 0) {
				addActionError("\u6CA1\u6709\u9700\u8981\u5BFC\u51FA\u7684\u6570\u636E\uFF01");
				return "success";
			} else {
				getClass();
				return downloadToExcel(attdSyncRecordList, "OAttendSync",
						outmatchModelId);
			}
		} else {
			return "success";
		}
	}

	private List searchattdSyncRecord() {
		DetachedCriteria dc = searchAttdSyncRec_DC();
		addCriteria(dc);
		IAttdSyncRecordBO attdSyncRecordBo = (IAttdSyncRecordBO) SpringBeanFactory
				.getBean("attdSyncRecordBO");
		List recordList = attdSyncRecordBo.getAttdSyncRecordList(dc, page);
		if (recordList.isEmpty())
			return recordList;
		Iterator i$ = recordList.iterator();
		do {
			if (!i$.hasNext())
				break;
			AttdSyncRecord record = (AttdSyncRecord) i$.next();
			if (record.getAsrEmp() != null
					&& (!StringUtils.isEmpty(record.getAsrEmpCardNo()) || !StringUtils
							.isEmpty(record.getAsrEmp().getEmpShiftNo())))
				if (!StringUtils.isEmpty(record.getAsrEmpCardNo())
						&& StringUtils.isEmpty(record.getAsrEmp()
								.getEmpShiftNo()))
					record.setAsrEmpCardNo((new StringBuilder()).append(
							record.getAsrEmpCardNo()).append("(-)").toString());
				else if (StringUtils.isEmpty(record.getAsrEmpCardNo())
						&& !StringUtils.isEmpty(record.getAsrEmp()
								.getEmpShiftNo()))
					record.setAsrEmpCardNo((new StringBuilder()).append("-(")
							.append(record.getAsrEmp().getEmpShiftNo()).append(
									")").toString());
				else if (!StringUtils.isEmpty(record.getAsrEmpCardNo())
						&& !StringUtils.isEmpty(record.getAsrEmp()
								.getEmpShiftNo())
						&& !record.getAsrEmpCardNo().equals(
								record.getAsrEmp().getEmpShiftNo()))
					record.setAsrEmpCardNo((new StringBuilder()).append(
							record.getAsrEmpCardNo()).append("(").append(
							record.getAsrEmp().getEmpShiftNo()).append(")")
							.toString());
		} while (true);
		return recordList;
	}

	private DetachedCriteria searchAttdSyncRec_DC() {
		DetachedCriteria dc = DetachedCriteria.forClass(AttdSyncRecord.class);
		dc.createAlias(AttdSyncRecord.PROP_ASR_EMP_NO, "emp", 1);
		dc.createAlias(AttdSyncRecord.PROP_ASR_ATTD_MACHINE_NO,
				"asrAttdMachine", 1);
		dc.createAlias((new StringBuilder()).append("emp.").append(
				Employee.PROP_EMP_DEPT_NO).toString(), "empDeptNo", 1);
		return dc;
	}

	private void addCriteria(DetachedCriteria dc) {
		if (startEmpCardNo != null && endEmpCardNo != null)
			dc.add(Restrictions.between((new StringBuilder()).append("emp.")
					.append(Employee.PROP_EMP_MACHINE_NO).toString(),
					startEmpCardNo, endEmpCardNo));
		else if (startEmpCardNo == null && endEmpCardNo != null)
			dc.add(Restrictions.le((new StringBuilder()).append("emp.").append(
					Employee.PROP_EMP_MACHINE_NO).toString(), endEmpCardNo));
		else if (startEmpCardNo != null && endEmpCardNo == null)
			dc.add(Restrictions.ge((new StringBuilder()).append("emp.").append(
					Employee.PROP_EMP_MACHINE_NO).toString(), startEmpCardNo));
		if (record == null)
			return;
		if (record.getAsrEmp() != null
				&& record.getAsrEmp().getEmpDeptNo() != null
				&& !StringUtils.isEmpty(record.getAsrEmp().getEmpDeptNo()
						.getId()))
			BaseCrit.addDeptDC(dc, (new StringBuilder()).append("emp.").append(
					Employee.PROP_EMP_DEPT_NO).toString(),
					(new StringBuilder()).append("emp.").append(
							Employee.PROP_EMP_PB_NO).toString(), null, record
							.getAsrEmp().getEmpDeptNo());
		if (record.getAsrSync() != null && record.getAsrSync().intValue() != 2)
			dc.add(Restrictions.eq(AttdSyncRecord.PROP_ASR_SYNC, record
					.getAsrSync()));
		if (record.getAsrAttdMachine() != null
				&& record.getAsrAttdMachine() != null)
			BaseCrit.addDC(dc, AttdSyncRecord.PROP_ASR_ATTD_MACHINE_NO,
					AttdMachine.PROP_MAC_ID, new Object[] { record
							.getAsrAttdMachine() });
		if (record.getAsrEmp() != null
				&& !StringUtils.isEmpty(record.getAsrEmp().getEmpName()))
			BaseCrit.addEmpDC(dc, "emp", record.getAsrEmp().getEmpName());
		if (record.getAsrAuthLevel() != null
				&& record.getAsrAuthLevel().intValue() != 2)
			dc.add(Restrictions.eq(AttdSyncRecord.PROP_ASR_AUTH_LEVEL, record
					.getAsrAuthLevel()));
		if (!StringUtils.isEmpty(record.getAsrEmpCardNo()))
			dc.add(Restrictions.ilike(AttdSyncRecord.PROP_ASR_CARD_NO,
					(new StringBuilder()).append("%").append(
							record.getAsrEmpCardNo()).append("%").toString()));
		if (record.getAsrEmp() != null
				&& record.getAsrEmp().getEmpStatus() != null
				&& record.getAsrEmp().getEmpStatus().intValue() != 2)
			dc.add(Restrictions.eq((new StringBuilder()).append("emp.").append(
					Employee.PROP_EMP_STATUS).toString(), record.getAsrEmp()
					.getEmpStatus()));
		if (!StringUtils.isEmpty(record.getAsrEmp().getEmpLocationNo().getId()))
			dc.add(Restrictions.eq((new StringBuilder()).append("emp.").append(
					Employee.PROP_EMP_LOCATION_NO).append(".id").toString(),
					record.getAsrEmp().getEmpLocationNo().getId()));
	}

	private void getDrillDownList() {
		departmentList = getDrillDown("Department", new String[0]);
		machineList = getDrillDown("AttdMachine", new String[0]);
		locations = getDrillDown("Location", new String[0]);
	}

	public Pager getPage() {
		return page;
	}

	public void setPage(Pager page) {
		this.page = page;
	}

	public List getAttdSyncRecordList() {
		return attdSyncRecordList;
	}

	public void setAttdSyncRecordList(List attdSyncRecordList) {
		this.attdSyncRecordList = attdSyncRecordList;
	}

	public List getDepartmentList() {
		return departmentList;
	}

	public void setDepartmentList(List departmentList) {
		this.departmentList = departmentList;
	}

	public List getMachineList() {
		return machineList;
	}

	public void setMachineList(List machineList) {
		this.machineList = machineList;
	}

	public String getOperate() {
		return operate;
	}

	public void setOperate(String operate) {
		this.operate = operate;
	}

	public String getAsrId() {
		return asrId;
	}

	public void setAsrId(String asrId) {
		this.asrId = asrId;
	}

	public AttdSyncRecord getRecord() {
		return record;
	}

	public void setRecord(AttdSyncRecord record) {
		this.record = record;
	}

	public Integer getStartEmpCardNo() {
		return startEmpCardNo;
	}

	public void setStartEmpCardNo(Integer startEmpCardNo) {
		this.startEmpCardNo = startEmpCardNo;
	}

	public Integer getEndEmpCardNo() {
		return endEmpCardNo;
	}

	public void setEndEmpCardNo(Integer endEmpCardNo) {
		this.endEmpCardNo = endEmpCardNo;
	}

	public List getLocations() {
		return locations;
	}

	public void setLocations(List locations) {
		this.locations = locations;
	}

	public String getSearchOrExport() {
		return searchOrExport;
	}

	public void setSearchOrExport(String searchOrExport) {
		this.searchOrExport = searchOrExport;
	}

	public String getOutmatchModelId() {
		return outmatchModelId;
	}

	public void setOutmatchModelId(String outmatchModelId) {
		this.outmatchModelId = outmatchModelId;
	}

	private static final long serialVersionUID = 0xdfc27e7b7714a9eeL;
	private AttdSyncRecord record;
	private Integer startEmpCardNo;
	private Integer endEmpCardNo;
	private List attdSyncRecordList;
	private List departmentList;
	private List machineList;
	private Pager page;
	private String asrId;
	private String operate;
	private List locations;
	private String searchOrExport;
	private String outmatchModelId;
	public final String outputIoName = "OAttendSync";
}
